﻿namespace DCHealthCheck.MvcWeb.Classes.DataServices {

    #region Usings
    using DCHealthCheck.Common;
    using DCHealthCheck.DomainModel;
    using DCHealthCheck.MvcWeb.Classes.ViewModels;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Linq.Expressions;
    using System.Threading;
    using System.Web; 
    #endregion

    public class HomeDataService : BaseDataService, IHomeDataService {

        #region Members
        private readonly Expression<Func<HealthCheckSummary, object>>[] HealthCheckSummaryIncludedEntities;

        private readonly IRepository<HealthCheckSummary, IEntityManager> HealthCheckSummaryRepository;
        #endregion
        
        #region Constructor
        public HomeDataService(
            IRepository<DomainController, IEntityManager> dcRepository,
            IRepository<HealthCheckSummary, IEntityManager> healthCheckSummaryRepository) : base(dcRepository) {

            this.HealthCheckSummaryRepository = healthCheckSummaryRepository;
            this.HealthCheckSummaryIncludedEntities = new Expression<Func<HealthCheckSummary, object>>[] {
                (x) => x.SystemInfos,
                (x) => x.SystemInfos.Select(y => y.DomainController)
            };
        }
        #endregion

        #region Methods
        public void ProcessData(HomeViewModel viewModel) {
            Global.DefaultLog.DebugFormat("[ThreadId: {0}] {1}", Thread.CurrentThread.ManagedThreadId, Extensions.CurrentMethodName());

            if (viewModel == null) {
                throw new ArgumentNullException("viewModel");
            }

            try {
                viewModel.Initialize();
                viewModel.HealthCheckSummaries =
                    this.HealthCheckSummaryRepository.Get(
                        x => ((x.CheckDateUTC >= viewModel.StartDate) && (x.CheckDateUTC <= viewModel.EndDate)),
                        this.HealthCheckSummaryIncludedEntities)
                    .ToList();

                viewModel.DisplayTotalResults = viewModel.HealthCheckSummaries.Count;

                if (!string.IsNullOrWhiteSpace(viewModel.SortColumn)) {
                    if (viewModel.CurrentSortDirection == System.Web.Helpers.SortDirection.Ascending) {
                        viewModel.HealthCheckSummaries = viewModel.HealthCheckSummaries
                            .OrderBy(viewModel.SortColumn)
                            .Skip(viewModel.SkipCount.Value)
                            .Take(viewModel.TakeCount.Value)
                            .ToList();
                    }
                    else {
                        viewModel.HealthCheckSummaries = viewModel.HealthCheckSummaries
                            .OrderByDescending(viewModel.SortColumn)
                            .Skip(viewModel.SkipCount.Value)
                            .Take(viewModel.TakeCount.Value)
                            .ToList();
                    }
                }
                else {
                    viewModel.HealthCheckSummaries = viewModel.HealthCheckSummaries
                        .OrderByDescending(x => x.CheckDateTimeUTC)
                        .Skip(viewModel.SkipCount.Value)
                        .Take(viewModel.TakeCount.Value)
                        .ToList();
                }

                viewModel.GetPageCounts();

            }
            catch (Exception e) {
                Global.DefaultLog.DebugFormat("[ThreadId: {0}] {1} {2}", Thread.CurrentThread.ManagedThreadId, Extensions.CurrentMethodName(), e.VerboseExceptionString());
            }
        } 
        #endregion
    }
}